//
//  CSGXDailyCashController.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/12/9.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXDailyCashController.h"
#import "CSGXHomeSaleCategoryView.h"
#import "CSGXCashDetailView.h"
#import "GXLinkViewController.h"
#import "CSGXDailySettingView.h"
#import "CSGXDatePicker.h"

#import "CSTextField.h"

#import "TGatheringDayDAL.h"

#import "TGatheringDayModel.h"

@interface CSGXDailyCashController () <CSGXCategoryViewActionDelegate, UITextFieldDelegate, CSGXDatePickerDelegate>

@property (nonatomic, weak) CSGXHomeSaleCategoryView *vSegment;

@property (nonatomic, strong) GXLinkViewController *linkVc;

@property (nonatomic, weak) UIView *vBody;

@property(nonatomic,weak) CSGXCashDetailView *vCashDetail;

@property (nonatomic, weak) CSGXDailySettingView *vSetting;

@property (nonatomic, weak) CSTextField *txtDay;
@property (nonatomic, weak) CSTextField *txtTimeBegin;
@property (nonatomic, weak) CSTextField *txtTimeEnd;

@property (nonatomic, weak) CSTextField *currentText;

@property (nonatomic, weak) CSGXDatePicker *datePicker;



@end

@implementation CSGXDailyCashController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSArray<TGatheringDayModel *> *gatheringList = [TGatheringDayDAL loadListWithTimeDay:[CSGXDateFormatterManager sharedManager].currentDay timeBegin:@"00:00" timeEnd:@"23:59"];
    if (gatheringList == nil || gatheringList.count == 0) {
        NSLog(@"gatheringList 没有数据！");
        return;
    }
    [self setDataToLinkView:gatheringList];
}


- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}





#pragma mark - Actions

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//    NSLog(@"textFieldShouldBeginEditing");
    [self dailyCashTextFieldDidClick:textField];
    return NO;
}

- (void)dailyCashTextFieldDidClick:(UITextField *)textField {
    self.currentText = (CSTextField *)textField;
    //alert DatePicker
    CSGXDatePicker *datePicker = [[CSGXDatePicker alloc] init];
    [[UIApplication sharedApplication].keyWindow addSubview:datePicker];
    self.datePicker = datePicker;
    
    [datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.size.mas_offset(datePicker.bounds.size);
    }];
    self.datePicker.delegate = self;
    
    if (self.currentText == self.txtDay) {
        datePicker.pickerType = PickerTypeDate;
        datePicker.title = [CSGXDateFormatterManager sharedManager].currentWeek;
    } else {
        datePicker.pickerType = PickerTypeTime;
        datePicker.title = @"设置时间";
    }

    
}

- (void)dailyCashTextFieldAction:(NSNotification *)note {
//    NSLog(@"dailyCashTextFieldAction");

    //select from db.
    NSString *tDate = self.txtDay.text;
    NSString *tBegin = self.txtTimeBegin.text;
    NSString *tEnd = self.txtTimeEnd.text;
    
    NSString *tFront = [tEnd subStringFrontOfKey:@":"];
    NSString *tLast = [tEnd subStringBackOfKey:@":"];
    if (tLast.length == 1) {
        tEnd = [NSString stringWithFormat:@"%@:0%@", tFront, tLast];
    }
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//    [fmt setTimeZone:[NSTimeZone localTimeZone]];
//    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    [fmt setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans_CN"]];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    NSString *str01 = [NSString stringWithFormat:@"%@ %@:00", tDate, tBegin];
    NSString *str02 = [NSString stringWithFormat:@"%@ %@:00", tDate, tEnd];
    
    NSDate * dateBegin = [fmt dateFromString:str01];
    NSDate * dateEnd = [fmt dateFromString:str02];

    // 判断合法性
    if (dateBegin > dateEnd) {
        NSLog("开始时间不能大于结束时间！");
        [CSGXProgressTool showErrorWithStatus:@"开始时间不能大于结束时间！" delay:1];
        [self setDataToLinkView:@[]];
        return;
    }

    NSArray<TGatheringDayModel *> *gatheringList = [TGatheringDayDAL loadListWithTimeDay:tDate timeBegin:tBegin timeEnd:tEnd];
    if (gatheringList == nil || gatheringList.count == 0) {
        NSLog(@"gatheringList 没有数据！");
//        return;
    }
    [self setDataToLinkView:gatheringList];
}



#pragma mark - methods
- (void)setDataToLinkView:(NSArray<TGatheringDayModel *> *)gatheringList {
    //@{@"tvPrefixHeaders":<#NSArray<NSString *> *#>, @"tvPrefixList":<#NSArray *#>, @"tvContentHeaders":<#NSArray<NSString *> *#>, @"tvContentList":<#NSArray *#>}
    
    //2条数据  2列
    //    self.linkVc.linkDict = @{@"tvPrefixHeaders":@[@"01", @"02"], @"tvPrefixList":@[
    //                                     @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"]],
    //                             @"tvContentHeaders":@[@"001", @"002"], @"tvContentList":@[
    //                                     @[@"001", @"002"], @[@"001", @"002"], @[@"001", @"002"]
    //                                     ]};
    
    
    NSArray *tvPrefixHeaders = @[@"行", @"客户"];
    //金额、上交款、刷卡、应收款、使用余额、优惠、刷卡费用、客户编号、客户类别
    NSArray *tvContentHeaders = @[@"金额", @"上交款", @"刷卡", @"应收款", @"使用余额", @"优惠", @"刷卡费用", @"客户编号", @"客户类别"];
    NSMutableArray<NSArray *> *tvPrefixList = [NSMutableArray<NSArray *> array];
    NSMutableArray<NSArray *> *tvContentList = [NSMutableArray<NSArray *> array];
    
    self.vCashDetail.dailyGatheringList = gatheringList;
    
    for (int i = 0; i < gatheringList.count; i++) {
        //
        [tvPrefixList addObject:@[@(i + 1).description, gatheringList[i].gBusinessman]];
        //
        [tvContentList addObject:@[
                                   [NSString stringWithFormat:@"%.2f", gatheringList[i].gGatheringMoney], //金额
                                   [NSString stringWithFormat:@"%.2f", gatheringList[i].gGatheringMoney], //上交款
                                   @"0.00", //刷卡
                                   @"0.00", //应收款
                                   @"0.00", //使用余额
                                   @"0.00", //优惠
                                   @"0.00", //刷卡费用
                                   @"0.00", //客户编号
                                   @"0.00", //客户类别
                                   ]];
        
    }
    self.linkVc.linkDict = @{@"tvPrefixHeaders": tvPrefixHeaders,
                             @"tvPrefixList": tvPrefixList,
                             @"tvContentHeaders": tvContentHeaders,
                             @"tvContentList": tvContentList
                             };
    
    
    //        //24条测试数据 10列
    //        self.linkVc.linkDict = @{@"tvPrefixHeaders":@[@"01", @"02"], @"tvPrefixList":@[
    //      @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"], @[@"AA", @"BBBBBBBBBBBBBBBB"]],
    //                                 @"tvContentHeaders":@[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @"tvContentList":@[
    //                                         @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"], @[@"001", @"002", @"003", @"004", @"005", @"006", @"007", @"008", @"009", @"010"]
    //                                         ]};
    
    
    
    
    //    self.linkVc.contentSvwWidth = 120; //test
    //    dispatch_async(dispatch_get_main_queue(), ^{
    //        [self.linkVc reload];
    //    });

}


#pragma mark - delegates
///CSGXCategoryViewActionDelegate
- (void)gxCategoryView:(CSGXHomeSaleCategoryView *)categoryView withSelectedIndex:(NSInteger)selectedIdnex {
    self.vSetting.hidden = !selectedIdnex;
    self.vBody.hidden = selectedIdnex;
}

- (void)gxDataPicker:(CSGXDatePicker *)datePicker time:(NSString *)time {
    if (datePicker.pickerType == PickerTypeDate) {
        //
        self.txtDay.text = time;
    } else {
        //
        self.currentText.text = time;
    }
    
    [self dailyCashTextFieldAction:nil];
}



#pragma mark - setupUI
- (void)setupUI {
    //segment
    CSGXHomeSaleCategoryView *vSegment = [[CSGXHomeSaleCategoryView alloc] init];
    [self.view addSubview:vSegment];
    
    vSegment.categoryActionDelegate = self;
    
    [vSegment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view).mas_offset(0);
        make.height.mas_offset(30);
    }];
    
    vSegment.segments = @[@"现金日报", @"设置"];
    
    //vTime
    UIView *vTime = [[UIView alloc] init];
    [self.view addSubview:vTime];
    
    [vTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view).mas_offset(0);
        make.top.equalTo(vSegment.mas_bottom).mas_offset(0);
        make.height.mas_offset(25);
    }];
    
    CSTextField *txtDay = [CSTextField gx_textFieldWithLineColor:nil lineWidth:2];
    [vTime addSubview:txtDay];
    self.txtDay = txtDay;
    
    [txtDay mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(vTime).mas_offset(0);
        make.leading.equalTo(vTime).mas_offset(5);
    }];
    txtDay.text = [CSGXDateFormatterManager sharedManager].currentDay;
    txtDay.delegate = self;
    
    CSTextField *txtTimeBegin = [CSTextField gx_textFieldWithLineColor:nil lineWidth:2];
    [vTime addSubview:txtTimeBegin];
    self.txtTimeBegin = txtTimeBegin;
    
    [txtTimeBegin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(vTime).mas_offset(0);
        make.left.equalTo(txtDay.mas_right).mas_offset(10);
        make.width.offset(50);
    }];
    txtTimeBegin.text = @"00:00";
    txtTimeBegin.delegate = self;
    
    UILabel *vTimeLine = [UILabel gx_labelWithText:@"-" fontSize:13 color:nil];
    [vTime addSubview:vTimeLine];
    
    [vTimeLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(vTime).mas_offset(0);
        make.left.equalTo(txtTimeBegin.mas_right).mas_offset(2);
    }];

    
    CSTextField *txtTimeEnd = [CSTextField gx_textFieldWithLineColor:nil lineWidth:2];
    [vTime addSubview:txtTimeEnd];
    self.txtTimeEnd = txtTimeEnd;
    
    [txtTimeEnd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(vTime).mas_offset(0);
        make.left.equalTo(vTimeLine.mas_right).mas_offset(2);
        make.width.offset(50);
    }];
    txtTimeEnd.text = @"23:59";
    txtTimeEnd.delegate = self;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dailyCashTextFieldAction:) name:UITextFieldTextDidChangeNotification object:nil];
    
    //vBody
    UIView *vBody = [[UIView alloc] init];
    [self.view addSubview:vBody];
    self.vBody = vBody;
    
    [vBody mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vTime.mas_bottom).mas_offset(0);
        make.leading.trailing.equalTo(self.view).mas_offset(0);
    }];
    
    GXLinkViewController *linkVc = [[GXLinkViewController alloc] init];
    linkVc.viewcontrollerEnum = ViewControllerDailyCash;
    [self gx_addChildController:linkVc intoView:vBody];
    
    [linkVc.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.trailing.equalTo(vBody).mas_offset(0);
    }];

    //底部明细汇总
    CSGXCashDetailView *vCashDetail = [[CSGXCashDetailView alloc] init];
    [self.view addSubview:vCashDetail];
    
    self.vCashDetail = vCashDetail;
    
    [vCashDetail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self.view).mas_offset(0);
        make.top.equalTo(vBody.mas_bottom).mas_offset(0);
    }];
    
    [self.view layoutIfNeeded];
    
    //
    self.vSegment = vSegment;
    self.linkVc = linkVc;
    
    
    //CSGXDailySettingView
    CSGXDailySettingView *vSetting = [[CSGXDailySettingView alloc] init];
    [self.view addSubview:vSetting];
    self.vSetting = vSetting;
    
//    vSetting.backgroundColor = [UIColor redColor];
    vSetting.hidden = YES;
    
    [vSetting mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vSegment.mas_bottom).mas_offset(0);
        make.leading.bottom.trailing.equalTo(self.view).mas_offset(0);
    }];

}












@end
